﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace HdipCricketClub.Models
{
    [MetadataType(typeof(ClubFixturePlayerMetaData))]
    public partial class ClubFixturePlayer
    {
        CricketClubEntities db = new CricketClubEntities();

        // properties

        public bool contact { get; set; }

        // repositories

        ClubFixturePlayerRepository clubFixturePlayerRepository = new ClubFixturePlayerRepository();
        PlayerSeasonStatisticRepository playerSeasonStatisticsRepository = new PlayerSeasonStatisticRepository();

        // methods

        public List<ClubFixturePlayer> CreateTeamForFixture(List<Member> members, Fixture fixture)
        {
            List<ClubFixturePlayer> playersAdded = new List<ClubFixturePlayer>();

            foreach (Member member in members)
            {
                ClubFixturePlayer fixturePlayer = new ClubFixturePlayer();

                fixturePlayer.fixture_id = fixture.fixture_id;
                fixturePlayer.player_id = member.member_id;
                fixturePlayer.contacted = false;
                fixturePlayer.contacted = false;

                db.ClubFixturePlayers.AddObject(fixturePlayer);
                db.SaveChanges();
                playersAdded.Add(fixturePlayer);
            }
            return playersAdded;
        }

        public List<ClubFixturePlayer> EditTeamForFixture(List<Member> members, Fixture fixture)
        {
            // get existing fixture team details

            IQueryable<ClubFixturePlayer> existingTeam = clubFixturePlayerRepository.GetClubFixturePlayers(fixture.fixture_id).AsQueryable();

            List<int> existingTeamPlayerIds = existingTeam.Select(et => et.player_id).ToList();

            // get selected fixture team details

            IQueryable<Member> selectedTeam = members.AsQueryable();

            List<int> selectedTeamIds = selectedTeam.Select(st => st.member_id).ToList();

            foreach (ClubFixturePlayer player in existingTeam)
            {
                if (!selectedTeamIds.Contains(player.player_id))
                {
                    ClubFixturePlayer clubFixturePlayer = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == player.fixture_id && cfp.player_id == player.player_id).Single();
                    db.ObjectStateManager.ChangeObjectState(clubFixturePlayer, EntityState.Deleted);
                    db.SaveChanges();
                }
            }

            foreach (Member member in selectedTeam)
            {
                if (!existingTeamPlayerIds.Contains(member.member_id))
                {
                    ClubFixturePlayer player = new ClubFixturePlayer()
                                                   {
                                                       fixture_id = fixture.fixture_id,
                                                       player_id = member.member_id
                                                   };

                    db.ClubFixturePlayers.AddObject(player);
                    db.SaveChanges();
                }
            }

            List<ClubFixturePlayer> revisedTeam = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == fixture.fixture_id)
                                                    .OrderBy(cfp => cfp.Member.last_name).ToList();

            return revisedTeam;
        }

        public List<ClubFixturePlayer> ParseFormCollectionForClubPlayersBattedFirst(FormCollection formCollection, Fixture fixture)
        {
            string[] fixtureId = formCollection.GetValues("item.fixture_id");
            string[] playerId = formCollection.GetValues("item.player_id");
            string[] batsmanNumber = formCollection.GetValues("item.batsman_number");
            string[] bowlerNumber = formCollection.GetValues("item.bowler_number");

            List<ClubFixturePlayer> clubPlayers = new List<ClubFixturePlayer>();

            for (int i = 0; i < fixture.no_of_players; i++)
            {
                int currentFixtureId = Int32.Parse(fixtureId[i]);
                int currentPlayerId = Int32.Parse(playerId[i]);

                ClubFixturePlayer player = clubFixturePlayerRepository.GetClubFixturePlayer(currentFixtureId, currentPlayerId);

                player.batsman_number = Int32.Parse(batsmanNumber[i]);

                if (bowlerNumber[i] != "")
                {
                    player.bowler_number = Int32.Parse(bowlerNumber[i]);
                }

                clubPlayers.Add(player);
            }

            return clubPlayers;
        }

        public List<ClubFixturePlayer> GetClubBowlers(List<ClubFixturePlayer> players)
        {
            List<ClubFixturePlayer> clubBowlers = new List<ClubFixturePlayer>();

            foreach (ClubFixturePlayer player in players)
            {
                if (player.bowler_number != null)
                {
                    clubBowlers.Add(player);
                }
            }

            List<ClubFixturePlayer> sortedClubBowlers = clubBowlers.OrderBy(cb => cb.bowler_number).ToList();

            return sortedClubBowlers;
        }

        public List<string> ValidateClubBatsmen(List<ClubFixturePlayer> clubBatsmen, Fixture fixture)
        {

            string message;

            List<String> errorMessages = new List<string>();

            List<int> batsmanNumbers = new List<int>();

            foreach (ClubFixturePlayer batsman in clubBatsmen)
            {
                if (batsman.batsman_number != null)
                {
                    batsmanNumbers.Add((int)batsman.batsman_number);
                }
            }

            if (batsmanNumbers.Count() == 0)
            {
                message = "No batsman numbers have been entered";
                errorMessages.Add(message);
            }

            else
            {
                batsmanNumbers.Sort();

                if (batsmanNumbers.First() != 1)
                {
                    message = "The first batsman numbers must be 1.";
                    errorMessages.Add(message);
                }

                if (batsmanNumbers.Last() != fixture.no_of_players)
                {
                    message = "The last batsman number must be equal to the number of players";
                    errorMessages.Add(message);
                }

                List<int> uniqueBatsmenId = new List<int>();
                List<int> duplicateBatsmenId = new List<int>();

                foreach (ClubFixturePlayer batsman in clubBatsmen)
                {
                    if (batsman.batsman_number != null)
                    {
                        if (uniqueBatsmenId.Contains((int)batsman.batsman_number))
                        {
                            duplicateBatsmenId.Add((int)batsman.batsman_number);
                        }
                        else
                        {
                            uniqueBatsmenId.Add((int)batsman.batsman_number);
                        }
                    }
                }

                //List<ClubFixturePlayer> duplicateBatsmen = new List<ClubFixturePlayer>();

                foreach (ClubFixturePlayer batsman in clubBatsmen)
                {
                    if (batsman.batsman_number != null)
                    {
                        if (duplicateBatsmenId.Contains((int)batsman.batsman_number))
                        {
                            message = "Batsman number " + batsman.batsman_number + " duplicated, " + batsman.Member.InitialAndLastName;
                            errorMessages.Add(message);
                        }
                    }
                }
            }
            return errorMessages;

        }

        public List<string> ValidateClubBowlers(List<ClubFixturePlayer> clubBowlers)
        {
            string message;

            List<string> errorMessages = new List<string>();

            List<int> bowlerNumbers = new List<int>();

            foreach (ClubFixturePlayer bowler in clubBowlers)
            {
                if (bowler.bowler_number != null)
                {
                    bowlerNumbers.Add((int)bowler.bowler_number);
                }
            }

            if (bowlerNumbers.Count() == 0)
            {
                message = "No bowler numbers have been entered";
                errorMessages.Add(message);
            }

            else
            {

                bowlerNumbers.Sort();

                if (bowlerNumbers.First() != 1)
                {
                    message = "The first bowler number must be 1";
                    errorMessages.Add(message);
                }
                if (bowlerNumbers.Last() != bowlerNumbers.Count())
                {
                    message = "The final bowler number must be equal to the number of bowlers";
                    errorMessages.Add(message);
                }

                List<int> uniqueBowlerNumbers = new List<int>();
                List<int> duplicateBowlerNumbers = new List<int>();

                foreach (ClubFixturePlayer bowler in clubBowlers)
                {
                    if (uniqueBowlerNumbers.Contains((int)bowler.bowler_number))
                    {
                        duplicateBowlerNumbers.Add((int)bowler.bowler_number);
                    }
                    else
                    {
                        uniqueBowlerNumbers.Add((int)bowler.bowler_number);
                    }
                }

                foreach (ClubFixturePlayer bowler in clubBowlers)
                {
                    if (duplicateBowlerNumbers.Contains((int)bowler.bowler_number))
                    {
                        message = "Bowler number " + bowler.bowler_number + " duplicated, " + bowler.Member.InitialAndLastName;
                        errorMessages.Add(message);
                    }
                }
            }
            return errorMessages;
        }

        public List<string> ValidateClubBatsmenScores(List<ClubFixturePlayer> clubBatsmen)
        {
            string message;

            List<string> errorMessages = new List<string>();

            foreach (ClubFixturePlayer batsman in clubBatsmen)
            {
                if (batsman.how_out == null)
                {
                    message = batsman.Member.InitialAndLastName + " how out must be entered";
                    errorMessages.Add(message);
                }

                if ((batsman.how_out == 1) || (batsman.how_out == 5) || (batsman.how_out == 7))
                {
                    if (batsman.fielder_name != null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " no fielder name should be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.bowler_name == null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is  " + batsman.DismissalType.type + " a bowler name must be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.batting_runs == null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " a value for runs must be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.batting_runs < 0)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " a value for runs cannot be less than 0";
                        errorMessages.Add(message);
                    }
                }

                if (batsman.how_out == 2)
                {
                    if (batsman.fielder_name != null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is lbw no fielder name should be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.bowler_name == null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is lbw a bowler name must be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.batting_runs == null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is lbw a value for runs must be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.batting_runs < 0)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is lbw a value for runs cannot be less than 0";
                        errorMessages.Add(message);
                    }
                }

                if ((batsman.how_out == 3) || (batsman.how_out == 4))
                {
                    if (batsman.fielder_name == null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " a fielder name should be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.bowler_name == null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " a bowler name must be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.batting_runs == null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " a value for runs must be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.batting_runs < 0)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " a value for runs cannot be less than 0";
                        errorMessages.Add(message);
                    }
                }

                if (batsman.how_out == 6)
                {
                    if (batsman.fielder_name != null)
                    {
                        message = batsman.Member.InitialAndLastName + "if dismissal type is " + batsman.DismissalType.type + " no fielder name should be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.bowler_name != null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " no bowler name should be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.batting_runs == null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " a value for runs must be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.batting_runs < 0)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " a value for runs cannot be less than 0";
                        errorMessages.Add(message);
                    }
                }

                if (batsman.how_out == 8)
                {
                    if (batsman.fielder_name != null)
                    {
                        message = batsman.Member.InitialAndLastName + "if dismissal type is " + batsman.DismissalType.type + " no fielder name should be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.bowler_name != null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " no bowler name should be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.batting_runs != null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " no value for runs must be entered";
                        errorMessages.Add(message);
                    }
                }

                if (batsman.how_out == 9)
                {
                    if (batsman.fielder_name != null)
                    {
                        message = batsman.Member.InitialAndLastName + "if dismissal type is " + batsman.DismissalType.type + " no fielder name should be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.bowler_name != null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " no bowler name should be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.batting_runs == null)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " a value for runs must be entered";
                        errorMessages.Add(message);
                    }
                    if (batsman.batting_runs < 0)
                    {
                        message = batsman.Member.InitialAndLastName + " if dismissal type is " + batsman.DismissalType.type + " a value for runs cannot be less than 0";
                        errorMessages.Add(message);
                    }
                }
            }
            return errorMessages;
        }

        public List<string> ValidateClubBowlersScores(List<ClubFixturePlayer> clubBowlers)
        {
            string message;

            List<string> errorMessages = new List<string>();

            foreach (ClubFixturePlayer bowler in clubBowlers)
            {
                if (bowler.overs == null)
                {
                    message = bowler.Member.InitialAndLastName + " no overs have been entered";
                    errorMessages.Add(message);
                }
                if (bowler.overs <= 0)
                {
                    message = bowler.Member.InitialAndLastName + " the number of overs must be greater than 0";
                    errorMessages.Add(message);
                }

                if (bowler.maidens > bowler.overs)
                {
                    message = bowler.Member.InitialAndLastName + " the number of maidens cannot exceed the numbers of overs";
                    errorMessages.Add(message);
                }

                if (bowler.bowling_runs < 0)
                {
                    message = bowler.Member.InitialAndLastName + " runs cannot be less than 0";
                    errorMessages.Add(message);
                }

                if (bowler.wickets < 0)
                {
                    message = bowler.Member.InitialAndLastName + " wickets cannot be less than 0";
                    errorMessages.Add(message);
                }
            }
            return errorMessages;
        }

        public double GetTotalBatsmenRuns(List<ClubFixturePlayer> clubBatsmen)
        {
            double totalBatsmenRuns = 0;

            foreach (ClubFixturePlayer batsman in clubBatsmen)
            {
                if (batsman.batting_runs != null)
                {
                    totalBatsmenRuns += (double)batsman.batting_runs;
                }
            }

            return totalBatsmenRuns;
        }

        public double GetTotalWicketsLost(List<ClubFixturePlayer> clubBatsmen)
        {
            double totalWicketsLost = 0;

            foreach (ClubFixturePlayer batsman in clubBatsmen)
            {
                if ((batsman.how_out != null) && (batsman.how_out != 8) && (batsman.how_out != 9))
                {
                    totalWicketsLost += 1;
                }
            }

            return totalWicketsLost;
        }

        public void UpdateClubBatsmen(List<ClubFixturePlayer> clubBatsmen)
        {
            foreach (ClubFixturePlayer batsman in clubBatsmen)
            {
                int fixtureID = batsman.fixture_id;
                int playerID = batsman.player_id;

                ClubFixturePlayer currentBatsman = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == fixtureID && cfp.player_id == playerID).Single();

                if (batsman.batsman_number != null)
                {
                    currentBatsman.batsman_number = batsman.batsman_number;
                }

                if (batsman.how_out != null)
                {
                    currentBatsman.how_out = batsman.how_out;
                }

                if (batsman.fielder_name != null)
                {
                    currentBatsman.fielder_name = batsman.fielder_name;
                }

                if (batsman.bowler_name != null)
                {
                    currentBatsman.bowler_name = batsman.bowler_name;
                }

                if (batsman.batting_runs != null)
                {
                    currentBatsman.batting_runs = batsman.batting_runs;
                }

                db.ObjectStateManager.ChangeObjectState(currentBatsman, EntityState.Modified);
                db.SaveChanges();
            }
        }

        public void UpdateClubBowlers(List<ClubFixturePlayer> clubBowlers)
        {
            foreach (ClubFixturePlayer bowler in clubBowlers)
            {
                int fixtureID = bowler.fixture_id;
                int playerID = bowler.player_id;

                ClubFixturePlayer currentBowler = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == fixtureID && cfp.player_id == playerID).Single();

                if (bowler.bowler_number != null)
                {
                    currentBowler.bowler_number = bowler.bowler_number;
                }

                if (bowler.overs != null)
                {
                    currentBowler.overs = bowler.overs;
                }

                if (bowler.maidens != null)
                {
                    currentBowler.maidens = bowler.maidens;
                }

                if (bowler.bowling_runs != null)
                {
                    currentBowler.bowling_runs = bowler.bowling_runs;
                }

                if (bowler.wickets != null)
                {
                    currentBowler.wickets = bowler.wickets;
                }

                db.ObjectStateManager.ChangeObjectState(currentBowler, EntityState.Modified);
                db.SaveChanges();
            }
        }

        public void UpdateBattingAndBowlingFiguresBattedFirst(Fixture fixture, FormCollection formCollection)
        {
            string[] fixtureId = formCollection.GetValues("item.fixture_id");
            string[] playerId = formCollection.GetValues("item.player_id");
            string[] batsmanNumber = formCollection.GetValues("item.batsman_number");
            string[] howOut = formCollection.GetValues("item.how_out");
            string[] fielderName = formCollection.GetValues("item.fielder_name");
            string[] bowlerName = formCollection.GetValues("item.bowler_name");
            string[] battingRuns = formCollection.GetValues("item.batting_runs");
            string[] bowlerNumber = formCollection.GetValues("item.bowler_number");
            string[] overs = formCollection.GetValues("item.overs");
            string[] maidens = formCollection.GetValues("item.maidens");
            string[] bowlingRuns = formCollection.GetValues("item.bowling_runs");
            string[] wickets = formCollection.GetValues("item.wickets");

            // update scorecard player's batting figures

            for (int i = 0; i < fixture.no_of_players; i++)
            {
                int currentFixture = Int32.Parse(fixtureId[i]);
                int currentPlayer = Int32.Parse(playerId[i]);

                ClubFixturePlayer clubBatsman = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == currentFixture && cfp.player_id == currentPlayer).Single();

                if (batsmanNumber[i] != "")
                {
                    clubBatsman.batsman_number = Int32.Parse(batsmanNumber[i]);
                }

                if (howOut[i] != "")
                {
                    clubBatsman.how_out = Int32.Parse(howOut[i]);
                }

                if (fielderName[i] != "")
                {
                    clubBatsman.fielder_name = fielderName[i];
                }

                if (bowlerName[i] != "")
                {
                    clubBatsman.bowler_name = bowlerName[i];
                }

                if (battingRuns[i] != "")
                {
                    clubBatsman.batting_runs = double.Parse(battingRuns[i]);
                }

                db.ObjectStateManager.ChangeObjectState(clubBatsman, EntityState.Modified);
                db.SaveChanges();
            }
            // update scorecard player's bowling figures

            for (int j = fixture.no_of_players; j < fixtureId.Length; j++)
            {
                int currentFixture = Int32.Parse(fixtureId[j]);
                int currentPlayer = Int32.Parse(playerId[j]);

                ClubFixturePlayer clubBowler = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == currentFixture && cfp.player_id == currentPlayer).Single();

                if (bowlerNumber[j - fixture.no_of_players] != "")
                {
                    clubBowler.bowler_number = Int32.Parse(bowlerNumber[j - fixture.no_of_players]);
                }

                if (overs[j - fixture.no_of_players] != "")
                {
                    clubBowler.overs = double.Parse(overs[j - fixture.no_of_players]);
                }

                if (maidens[j - fixture.no_of_players] != "")
                {
                    clubBowler.maidens = double.Parse(maidens[j - fixture.no_of_players]);
                }

                if (bowlingRuns[j - fixture.no_of_players] != "")
                {
                    clubBowler.bowling_runs = double.Parse(bowlingRuns[j - fixture.no_of_players]);
                }

                if (wickets[j - fixture.no_of_players] != "")
                {
                    clubBowler.wickets = double.Parse(wickets[j - fixture.no_of_players]);
                }

                db.ObjectStateManager.ChangeObjectState(clubBowler, EntityState.Modified);
                db.SaveChanges();
            }

            // update season statistics

            List<int> playersWithSeasonStatistics = playerSeasonStatisticsRepository.GetPlayerIds();

            for (int k = 0; k < fixture.no_of_players; k++)
            {
                int scorecardPlayerId = Int32.Parse(playerId[k]);

                ClubFixturePlayer clubFixturePlayer = clubFixturePlayerRepository.GetClubFixturePlayer(fixture.fixture_id, scorecardPlayerId);

                if (playersWithSeasonStatistics.Contains(scorecardPlayerId))
                {

                    PlayerSeasonStatistic scorecardPlayer = db.PlayerSeasonStatistics.Where(pss => pss.player_id == scorecardPlayerId).Single();

                    scorecardPlayer.matches += 1;

                    if (clubFixturePlayer.how_out != 8)
                    {
                        scorecardPlayer.innings += 1;
                    }

                    if (clubFixturePlayer.how_out == 9)
                    {
                        scorecardPlayer.not_out += 1;
                    }

                    if (clubFixturePlayer.batting_runs != null)
                    {
                        scorecardPlayer.batting_runs += clubFixturePlayer.batting_runs;

                        if (clubFixturePlayer.batting_runs > scorecardPlayer.best_batting)
                        {
                            scorecardPlayer.best_batting = clubFixturePlayer.batting_runs;
                        }
                    }

                    if (clubFixturePlayer.overs != null)
                    {
                        scorecardPlayer.overs += clubFixturePlayer.overs;
                    }

                    if (clubFixturePlayer.maidens != null)
                    {
                        scorecardPlayer.maidens += clubFixturePlayer.maidens;
                    }

                    if (clubFixturePlayer.bowling_runs != null)
                    {
                        scorecardPlayer.bowling_runs += clubFixturePlayer.bowling_runs;
                    }

                    if (clubFixturePlayer.wickets != null)
                    {
                        scorecardPlayer.wickets += clubFixturePlayer.wickets;
                    }

                    if ((clubFixturePlayer.wickets > scorecardPlayer.best_bowling_wickets) && (clubFixturePlayer.bowling_runs < scorecardPlayer.best_bowling_runs))
                    {
                        scorecardPlayer.best_bowling_wickets = clubFixturePlayer.wickets;
                        scorecardPlayer.best_bowling_runs = clubFixturePlayer.bowling_runs;
                    }

                    db.ObjectStateManager.ChangeObjectState(scorecardPlayer, EntityState.Modified);
                    db.SaveChanges();
                }

                if (!playersWithSeasonStatistics.Contains(scorecardPlayerId))
                {
                    PlayerSeasonStatistic scorecardPlayer = new PlayerSeasonStatistic() { player_id = scorecardPlayerId };

                    scorecardPlayer.matches = 1;

                    if (clubFixturePlayer.how_out != 8)
                    {
                        scorecardPlayer.innings = 1;
                    }

                    if (clubFixturePlayer.how_out == 9)
                    {
                        scorecardPlayer.not_out = 1;
                    }

                    if (clubFixturePlayer.batting_runs != null)
                    {
                        scorecardPlayer.batting_runs = clubFixturePlayer.batting_runs;
                        scorecardPlayer.best_batting = clubFixturePlayer.batting_runs;
                    }

                    if (clubFixturePlayer.overs != null)
                    {
                        scorecardPlayer.overs = clubFixturePlayer.overs;
                    }

                    if (clubFixturePlayer.maidens != null)
                    {
                        scorecardPlayer.maidens = clubFixturePlayer.maidens;
                    }

                    if (clubFixturePlayer.bowling_runs != null)
                    {
                        scorecardPlayer.bowling_runs = clubFixturePlayer.bowling_runs;
                        scorecardPlayer.best_bowling_runs = clubFixturePlayer.bowling_runs;
                    }

                    if (clubFixturePlayer.wickets != null)
                    {
                        scorecardPlayer.wickets = clubFixturePlayer.wickets;
                        scorecardPlayer.best_bowling_wickets = clubFixturePlayer.wickets;
                    }

                    db.PlayerSeasonStatistics.AddObject(scorecardPlayer);
                    db.SaveChanges();
                }
            }
        }

        public void EditClubFixtureBatsmen(List<ClubFixturePlayer> clubPlayers)
        {
            foreach (ClubFixturePlayer player in clubPlayers)
            {
                ClubFixturePlayer currentPlayer = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == player.fixture_id && cfp.player_id == player.player_id).Single();

                currentPlayer.batsman_number = player.batsman_number;
                currentPlayer.how_out = player.how_out;
                currentPlayer.fielder_name = player.fielder_name;
                currentPlayer.bowler_name = player.bowler_name;
                currentPlayer.batting_runs = player.batting_runs;

                db.ObjectStateManager.ChangeObjectState(currentPlayer, EntityState.Modified);
                db.SaveChanges();
            }
        }

        public void ResetClubFixturePlayerDetails(int Id)
        {
            List<ClubFixturePlayer> fixturePlayers = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == Id).ToList();

            foreach (ClubFixturePlayer player in fixturePlayers)
            {
                if (player.batsman_number != null)
                {
                    player.batsman_number = null;
                }

                if (player.how_out != null)
                {
                    player.how_out = null;
                }

                if (player.fielder_name != null)
                {
                    player.fielder_name = null;
                }

                if (player.bowler_name != null)
                {
                    player.bowler_name = null;
                }

                if (player.batting_runs != null)
                {
                    player.batting_runs = null;
                }

                if (player.bowler_number != null)
                {
                    player.bowler_number = null;
                }

                if (player.overs != null)
                {
                    player.overs = null;
                }

                if (player.maidens != null)
                {
                    player.maidens = null;
                }

                if (player.bowling_runs != null)
                {
                    player.bowling_runs = null;
                }

                if (player.wickets != null)
                {
                    player.wickets = null;
                }

                db.ObjectStateManager.ChangeObjectState(player, EntityState.Modified);
                db.SaveChanges();
            }
        }
        
        public void EditClubFixtureBowlers(List<ClubFixturePlayer> clubPlayers)
        {
            foreach (ClubFixturePlayer player in clubPlayers)
            {
                ClubFixturePlayer currentPlayer = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == player.fixture_id && cfp.player_id == player.player_id).Single();

                currentPlayer.overs = player.overs;
                currentPlayer.maidens = player.maidens;
                currentPlayer.bowling_runs = player.bowling_runs;
                currentPlayer.wickets = player.wickets;

                db.ObjectStateManager.ChangeObjectState(currentPlayer, EntityState.Modified);
                db.SaveChanges();
            }
        }


    }

    public class ClubFixturePlayerMetaData
    {
        [Required(ErrorMessage = "You must select a unique player from the list:")]
        public int player_id;

        //[Required]
        [Display(Name = "Batsman Number:")]
        [Range(1, 11, ErrorMessage = "Batman number must be in range 1 to 11")]
        public int? batsman_number;

        [Required]
        [Display(Name = "How Out:")]
        public int how_out;

        [Display(Name = "Fielder Name:")]
        public int fielder_name;

        [Display(Name = "Bowler Name:")]
        public int bowler_name;

        [Display(Name = "Batting Runs:")]
        [Range(0, 300, ErrorMessage = "Runs scored must be greater than or equal to zero")]
        public double batting_runs;

        [Display(Name = "Bowler Number:")]
        [Range(1, 11, ErrorMessage = "Bowler number must be in the range 1 to 11")]
        public int bowler_number;

        [Display(Name = "Overs:")]
        [Range(0, 300, ErrorMessage = "Overs must be greater than or equal to zero")]
        public double overs;

        [Display(Name = "Maidens:")]
        [Range(0, 300, ErrorMessage = "Maidens must be greater than or equal to zero")]
        public double maidens;

        [Display(Name = "Bowling Runs:")]
        [Range(0, 300, ErrorMessage = "Runs must be greater than or equal to zero")]
        public double bowling_runs;

        [Display(Name = "Wickets:")]
        [Range(0, 10, ErrorMessage = "Wickets taken must be in range 1 to 10")]
        public double wickets;

        [Display(Name = "Contact:")]
        public bool contact;

        [Display(Name = "Contacted:")]
        public bool contacted;

        [Display(Name = "Confirmed")]
        public bool confirmed;

    }

}